<%@tag import="org.apache.solr.search.Grouping.TotalCount"%>
<%@	tag pageEncoding="UTF-8"%>
<%@ attribute name="page" type="com.github.miemiedev.mybatis.paginator.domain.PageList" required="true"%>
<%@ attribute name="paginationSize" type="java.lang.Integer" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
if(paginationSize == null){
	paginationSize = 10;
}
int first = 1;
int slider = 1;
int current =  page.getPaginator().getPage();
int begin = Math.max(1, current - paginationSize/2);
int end = Math.min(begin + (paginationSize - 1), page.getPaginator().getTotalPages());
int pageSize = page.getPaginator().getLimit();
int count = page.getPaginator().getTotalCount();
int last = page.getPaginator().getTotalPages();

request.setAttribute("pageSize", pageSize);
request.setAttribute("current", current);
request.setAttribute("begin", begin);
request.setAttribute("end", end);
request.setAttribute("totalPages", page.getPaginator().getTotalPages());
request.setAttribute("totalCount", count);

%>
<div class="pagination">
	<ul>
		 <% if (page.getPaginator().isFirstPage()){%>
         		<li class="disabled"><a href="javascript:">&#171; 上一页</a></li>
         <%}else{%>
         		<li><a href="javascript:" onclick="page(<%=page.getPaginator().getPrePage() %>,<%=pageSize %>);">&#171; 上一页</a></li>
         <%} 
		 	begin = current - (paginationSize / 2);

			if (begin < first) {
				begin = first;
			}

			end = begin + paginationSize - 1;

			if (end >= last) {
				end = last;
				begin = end - paginationSize + 1;
				if (begin < first) {
					begin = first;
				}
			}
			
 			if(begin > first){
 				int i = 0;
 				for (i = first; i < first + slider && i < begin; i++) {
 					%>
 					<li><a href="javascript:" onclick="page(<%=i %>,<%=pageSize%>);"><%=i+1-first %></a></li>
 					<%
 				}
 				if (i < begin) {
 					%>
 					<li class="disabled"><a href="javascript:">...</a></li>
 					<%
 				}
 			}
	        for (int i = begin; i <= end; i++) {
	        	if (i == current) {
	        		%>
	        		<li class="active"><a href="javascript:"><%=i %></a></li>
	        		<%
	        	}else{
	        		%>
	        		<li><a href="javascript:" onclick="page(<%=i %>,<%=pageSize %>);"><%=i %></a></li>
	        		<%
	        	}
	        }
		  	if (last - end > slider) {
				%>
		  		<li class="disabled"><a href="javascript:">...</a></li>
		  		<%
				end = last - slider;
			}
			
			for (int i = end + 1; i <= last; i++) {
				%>
				<li><a href="javascript:" onclick="page(<%=i %>,<%=pageSize %>);"><%=i+1-first %></a></li>
				<%
			}
			 if (page.getPaginator().isLastPage()){%>
               	<li class="disabled"><a href="javascript:">下一页 &#187;</a></li>
	         <%}else{%>
	                <li><a href="javascript:" onclick="page(<%=page.getPaginator().getNextPage()%>,<%=pageSize %>);">下一页 &#187;</a></li>
	         <%} %>
			<li class="disabled controls"><a href="javascript:">当前 
			<input type="text" value="<%=current %>" onkeypress="var e=window.event||this;var c=e.keyCode||e.which;if(c==13)page(this.value,<%=pageSize %>);" onclick="this.select();"/> / 
			<input type="text" value="<%=pageSize %>" onkeypress="var e=window.event||this;var c=e.keyCode||e.which;if(c==13)page(<%=current %>,this.value);" onclick="this.select();"/> 条，共<%=count %> 条</a><li>
	</ul>
	<div style="clear:both;"></div>
</div>
